{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "3c5c3807-6c6c-450b-96ae-3dd7d268b3e9",
   "metadata": {},
   "source": [
    "## 在notebook中，输出变量可以直接写变量名，不用写 print"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "e564320e-23bd-4c61-b339-8408dc929bff",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a=5\n",
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "387dff28-1cc4-48d4-923e-004f0e8c29f4",
   "metadata": {},
   "source": [
    "## 常规方法生成一个列表，是要这样弄"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a00e5a80-e1a5-4828-aac2-ef62f7038214",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 2, 4, 6, 8]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "L=[]\n",
    "def my_func(x):\n",
    "    return 2*x\n",
    "\n",
    "for i in range(5):\n",
    "    L.append(my_func(i))\n",
    "\n",
    "L    "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a7044aab-5852-4a86-b9f4-1f26a6b36a24",
   "metadata": {},
   "source": [
    "## 但更好的方法是使用列表式进行推导\n",
    "### 它的写法是 [* for i in *] , 第一个*放入函数，第二个*放入迭代对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "f8bc856d-3c7a-4da4-a5fe-945cffa185a9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 2, 4, 6, 8]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[my_func(i) for i in range(5)]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "52481471-1663-4039-9146-3c5d5b3c9ce2",
   "metadata": {},
   "source": [
    "## 还可以嵌套\n",
    "### 代码解读：可以把 m + '_' + n ，看成是一个函数，即把两个字符串用下划线拼起来\n",
    "### 然后就套了个双重循环，外层循环 a,b ， 内层循环 c,d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "2031dfc4-b2a5-4830-baf9-dba319b24467",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a_c', 'a_d', 'b_c', 'b_d']"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[m+'_'+n for m in ['a','b'] for n in['c','d']]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6b0310e9-9dd5-4558-9b39-6054fc7ed4c4",
   "metadata": {},
   "source": [
    "# if 的语法糖用于赋值，其实就是Python中的三元表达式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "0d549063-4f01-45d3-84ce-782f83aee211",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'cat'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "value = 'cat' if 2>1 else 'dog'\n",
    "value"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "813f8459-d906-4de7-931d-8d469132c88d",
   "metadata": {},
   "source": [
    "## 综合运用前面的技巧\n",
    "### 实现的功能是，将列表中大于5的数设置为5，其它的保持原来的样子"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "7fdd3da9-2abb-4af5-81cd-2cf0edffa405",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 5, 5]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "L=[1,2,3,4,5,6,7]\n",
    "L2=[ i if i<=5 else 5 for i in L]\n",
    "L2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1d098d51-c4d2-43cf-8001-1381d21ca1a5",
   "metadata": {},
   "source": [
    "## lambda的应用 \n",
    "### （lambda x:2*x） 可以理解为就是一个函数，要调用这个函数，当然就是要在它后面加上小括号和参数，也就是(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "6862482b-df17-4c97-96ce-719c3183a9c5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 2, 4, 6, 8]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[(lambda x: 2*x)(i) for i in range(5)]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "56205e90-9a15-4ca1-9e4d-86fefc69de1a",
   "metadata": {},
   "source": [
    "## map 函数的使用 map(一个函数,一个可迭代的对象）\n",
    "### map 函数返回的是一个map对象，（惰性迭代器），通常要用 list() 来转换成列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "3a54117b-a55a-409d-9fff-49c41826d66a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 2, 4, 6, 8]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(map(lambda x:2*x,range(5)))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b0de446d-52a0-4d1d-b47d-294c6930d462",
   "metadata": {},
   "source": [
    "## map 有多个值的情况\n",
    "### 这一行代码的详解\n",
    "\n",
    "**参数**\n",
    "range(5)\n",
    "生成序列：[0, 1, 2, 3, 4]\n",
    "\n",
    "list('abcde')\n",
    "把字符串 'abcde' 转成列表：['a', 'b', 'c', 'd', 'e']\n",
    "\n",
    "**lambda**\n",
    "\n",
    "lambda x, y: str(x)+'_'+y\n",
    "匿名函数，接受两个参数 x 和 y，返回一个字符串：\n",
    "\n",
    "**. map 的执行逻辑**\n",
    "\n",
    "map() 会把 range(5) 和 ['a','b','c','d','e'] 这两个序列 逐个配对，依次传给 lambda：\n",
    "\n",
    "第 1 次：x=0, y='a' → '0_a'\n",
    "\n",
    "第 2 次：x=1, y='b' → '1_b'\n",
    "\n",
    "第 3 次：x=2, y='c' → '2_c'\n",
    "\n",
    "第 4 次：x=3, y='d' → '3_d'\n",
    "\n",
    "第 5 次：x=4, y='e' → '4_e'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "a60e2823-48ce-4d41-8f43-9b0a638a6b9d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['0_a', '1_b', '2_c', '3_d', '4_e']"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(map(lambda x,y:str(x)+'_'+y,range(5),list('abcde')))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f2c61722-b5ac-4a7b-b1c1-f37569321eb0",
   "metadata": {},
   "source": [
    "## zip\n",
    "zip() 会把多个可迭代对象（list, tuple, string 等）“一一配对”，打包成元组。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "28876f10-e74e-4292-ba20-7bc7d8f0e501",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Tom 90\n",
      "Jerry 85\n",
      "Spike 78\n"
     ]
    }
   ],
   "source": [
    "names =['Tom','Jerry','Spike']\n",
    "scores=[90,85,78]\n",
    "\n",
    "for name,score in zip(names,scores):\n",
    "    print(name,score)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0d790797-d325-417a-80e0-ab382594abb0",
   "metadata": {},
   "source": [
    "### 代码解释\n",
    "第一次：取 L1[0]、L2[0]、L3[0] → ('a', 'd', 'h')\n",
    "\n",
    "第二次：取 L1[1]、L2[1]、L3[1] → ('b', 'e', 'i')\n",
    "\n",
    "第三次：取 L1[2]、L2[2]、L3[2] → ('c', 'f', 'j')\n",
    "\n",
    "zip 返回一个 迭代器（不是 list），所以一般要用 list() 转换才能看到结果。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "79c913e4-2616-4e69-91b9-9f05e395e563",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('a', 'd', 'h'), ('b', 'e', 'i'), ('c', 'f', 'j')]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "L1, L2, L3 = list('abc'), list('def'), list('hij') \n",
    "list(zip(L1, L2, L3))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c34092c6-7475-4f9d-acb4-79c6bafbd59a",
   "metadata": {},
   "source": [
    "## enumerate\n",
    "enumerate 是一种特殊的打包，它可以在迭代时绑定迭代元素的遍历序号："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "5be2a572-b3c9-4745-b17c-3ae466cd52a5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 a\n",
      "1 b\n",
      "2 c\n",
      "3 d\n"
     ]
    }
   ],
   "source": [
    "L=list('abcd')\n",
    "for index,value in enumerate(L):\n",
    "    print(index,value)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "77abe22b-6415-459b-90f4-3112a644dd00",
   "metadata": {},
   "source": [
    "## 用zip 也能实现上面 enumerate的功能"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "2587a943-9352-46dc-b97e-83954461cf30",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 a\n",
      "1 b\n",
      "2 c\n",
      "3 d\n"
     ]
    }
   ],
   "source": [
    "for index,value in zip(range(len(L)),L):\n",
    "    print(index,value)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "32445cd2-2db4-4c09-adc1-0fa22a6448c4",
   "metadata": {},
   "source": [
    "## 用ZIP给两个列表建立字典映射\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "2f54b4c6-12c5-4bd0-9238-ffbb3903e6b7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1: 'Wang', 2: 'Zhang', 3: 'Li'}"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "idList=[1,2,3]\n",
    "nameList=['Wang','Zhang','Li']\n",
    "dict(zip(idList,nameList))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "22cc520d-d4ef-49a1-b2a7-b091e13c56e0",
   "metadata": {},
   "source": [
    "## 用*号给 zip 解压缩"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "4bef37bd-49d6-45a7-a83f-62805c4e3213",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "zipped 结果 [('a', 'd', 'h'), ('b', 'e', 'i'), ('c', 'f', 'j')]\n",
      "解压的结果 [('a', 'b', 'c'), ('d', 'e', 'f'), ('h', 'i', 'j')]\n"
     ]
    }
   ],
   "source": [
    "zipped= list(zip(L1,L2,L3))\n",
    "print(\"zipped 结果\",zipped)\n",
    "unzip=list(zip(*zipped)) # 三个元组分别对应原来的列表\n",
    "print(\"解压的结果\",unzip)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "da09cc65-9299-4cc8-912d-f7abdd877b8e",
   "metadata": {},
   "source": [
    "# Numpy\n",
    "## 普通列表，转成Numpy数组对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "39089853-c7a1-48a0-87d4-d90e21d4b0fb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3])"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "np.array([1,2,3])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0a70cb3f-5e8b-40d0-ac7e-f2a2fe3753ed",
   "metadata": {},
   "source": [
    "## 多维数组\n",
    "### 2行3列的矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "ff1e5df9-fb47-4708-bd20-753f3f9339ac",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mat = np.array([[1,2,3],[4,5,6]])\n",
    "mat"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "78151dd2-efb2-4854-8203-c607d3dfafd8",
   "metadata": {},
   "source": [
    "## 数学运算\n",
    "### Numpy的强大之处： 运算会自动作用在整个数组上"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "fecbf473-377e-446e-aa9f-53e3aabcafba",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[11 12 13 14]\n",
      "[2 4 6 8]\n",
      "[ 1  4  9 16]\n"
     ]
    }
   ],
   "source": [
    "arr=np.array([1,2,3,4])\n",
    "print(arr+10)\n",
    "print(arr*2)\n",
    "print(arr**2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "51e7733c-d82b-40c5-96b2-24418b6bc68d",
   "metadata": {},
   "source": [
    "## Numpy 的常用函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "dfe0fa00-46e5-4d9e-866f-2995035ee397",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n",
      "2.5\n",
      "4\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "print(np.sum(arr)) # 求和\n",
    "print(np.mean(arr)) # 求平均值\n",
    "print(np.max(arr)) # 求最大\n",
    "print(np.min(arr)) # 求最小"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0029127c-5d84-48f2-8d23-237c08ac7d8b",
   "metadata": {},
   "source": [
    "## 创建等差数列\n",
    "### 方法一，使用arange"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "ca7aa850-f8d7-4151-9b54-26af5861d0ac",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  2,  4,  6,  8, 10, 12, 14, 16, 18])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 从0开始（含），到20结束（不含 ），步长为2\n",
    "np.arange(0,20,2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "41583850-58f2-431a-a502-af4f29fffcf8",
   "metadata": {},
   "source": [
    "### 方法二，使用linspace"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "abcecc12-8378-478d-b0f1-fe57032a299c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0. ,  2.5,  5. ,  7.5, 10. ])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.linspace(0,10,5) #范围是从0到10，一共生成5个数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9c84caa9-e13b-4529-b147-47f5ea621a18",
   "metadata": {},
   "source": [
    "## 特殊数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "25dea376-6b67-48ef-a5a7-e497ca1b5af7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0., 0., 0.],\n",
       "       [0., 0., 0.]])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.zeros((2,3))# 2x3 全零矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "ce19c80c-0061-4a55-b310-6f9c12089ed2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 1.],\n",
       "       [1., 1.]])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.ones((2,2))    # 2x2 全一矩阵"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "60c8d795-8925-4ecb-918e-d2b69a1a8a94",
   "metadata": {},
   "source": [
    "### 单位矩阵（Identity Matrix） \n",
    "\n",
    "是一种特殊的 方阵（行数m = 列数）：\n",
    "\n",
    "主对角线（左上到右下）上的元素全是 1\n",
    "\n",
    "其他位置的元素全是 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "4a592510-430d-4525-ade3-ed5fe184414b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 0., 0., 0., 0.],\n",
       "       [0., 1., 0., 0., 0.],\n",
       "       [0., 0., 1., 0., 0.],\n",
       "       [0., 0., 0., 1., 0.],\n",
       "       [0., 0., 0., 0., 1.]])"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.eye(5)        # 单位矩阵 (5x5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "66aea785-eec7-434f-8977-bbfcb0bb366a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[10, 10, 10],\n",
       "       [10, 10, 10]])"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.full((2,3),10) # 创建一个2*3的矩阵，全部用10来填充"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "3eccee78-469f-496b-847d-188885064b9b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[7, 8, 9],\n",
       "       [7, 8, 9],\n",
       "       [7, 8, 9],\n",
       "       [7, 8, 9],\n",
       "       [7, 8, 9]])"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.full((5,3),[7,8,9]) # 创建一个5行3列的矩阵，每一行都填充 7,8,9"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8e7d76fb-3a13-45ed-86ff-4dcb38b177dc",
   "metadata": {},
   "source": [
    "## 随机矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "794fd8cf-a5b4-40df-9f7c-80641a2c7d00",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.74733674, 0.44944324, 0.64146193, 0.1077481 , 0.79076791,\n",
       "       0.80035031, 0.82129307, 0.32239646])"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.rand(8) # 生成0-1之间，均匀分布的8个随机数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "bfb6e8f0-0041-4170-889f-7fe3b195ebc9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.71906616, 0.20657114, 0.74495302],\n",
       "       [0.62496246, 0.82743993, 0.13014792],\n",
       "       [0.51846402, 0.6340264 , 0.55538967]])"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.rand(3,3) # 创建一个3*3的矩阵，并用随机数填充 注意这里传的不是元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "4c09e681-8ce1-4943-831d-df90cc7152fd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([12.07236202,  5.54169407,  6.18640919])"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.uniform(5, 15, 3) # 生成3个范围在5-15之间的随机数组"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "359afbfd-7869-4de5-b88b-a12215ee88a4",
   "metadata": {},
   "source": [
    "### 标准正态分布\n",
    "- np.random.rand(3) → 在 [0,1) 区间均匀分布的随机数。\n",
    "\n",
    "- np.random.randn(3) → 在 (-∞, +∞) 范围，服从 标准正态分布的随机数。数据分布是 钟形曲线，大部分值集中在 0 附近，越远离 0 出现的概率越小。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "d6bac88e-e813-4d23-a0c9-85ec51463e17",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-1.33527161,  0.2108075 , -1.08169157])"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.randn(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "90338968-24da-4e23-9e42-071f8891df1c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.47441873, -0.54697447,  1.05188677],\n",
       "       [ 0.76978331,  1.76736273,  0.43941226]])"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.randn(2,3) # 一样的可以生成矩阵"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7e4007ca-86f0-4e2b-9b3f-bc8de3762286",
   "metadata": {},
   "source": [
    "## 随机整数\n",
    "### 生成5到14的 随机数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "c1e97378-5945-4990-ab20-de6e199568c5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 6, 10, 14],\n",
       "       [ 9, 13, 10],\n",
       "       [10,  8,  5]], dtype=int32)"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "low,high,size=5,15,(3,3)\n",
    "np.random.randint(low,high,size)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aecb45fc-0ae7-4c81-a25f-c9d7bc06aac5",
   "metadata": {},
   "source": [
    "## 从列表中抽取\n",
    "np.random.choice(a, size=None, replace=True, p=None)\n",
    "参数说明：\n",
    "\n",
    "a 要抽的列表\n",
    "\n",
    "replace：是否允许重复抽取\n",
    "\n",
    "True（默认）= 有放回抽样，可以重复\n",
    "\n",
    "False = 无放回抽样，不会重复\n",
    "\n",
    "p：每个元素被选中的概率（默认均匀概率）。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "f1275dfd-6a9d-4750-8ea7-862268777a9f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Tom' 'Spike']\n"
     ]
    }
   ],
   "source": [
    "names = ['Tom', 'Jerry', 'Spike']\n",
    "print(np.random.choice(names, 2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "fa39c047-8770-4d5b-93bb-75325917371e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[4 4 1]\n"
     ]
    }
   ],
   "source": [
    "# 从 [0,1,2,3,4] 中随机抽 3 个。 \n",
    "print(np.random.choice(5, 3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "7d93264b-e979-4a8b-a3d6-85be363d30bc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[4 5 5 1 5 6 5 1 5 5]\n"
     ]
    }
   ],
   "source": [
    "# 骰子，每个点数概率不一样\n",
    "dice = [1, 2, 3, 4, 5, 6]\n",
    "p = [0.1, 0.1, 0.1, 0.1, 0.5, 0.1]  # 5点更容易出现 ，所有值必须加起来等于1\n",
    "print(np.random.choice(dice, 10, p=p))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d4dab73c-27e4-4a42-bd2c-ca24234318ab",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:base] *",
   "language": "python",
   "name": "conda-base-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
